<template>
  <div>
    <div style="padding: 10px 0">
      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-search" v-model="answer"></el-input>
      <el-button class="ml-5" type="success" @click="load">搜索</el-button>
      <el-button class="ml-5" type="warning" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-button type="success" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
      <el-button type="danger" @click="deleteBatch">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      <el-upload action="http://localhost:8082/reply/import"
                 :show-file-list="false"
                 accept=".xlsx"
                 :on-success="handleExcelImportSuccess"
                 style="display: inline-block">
        <el-button type="success" class="ml-5">导入 <i class="el-icon-download"></i></el-button>
      </el-upload>
      <el-button type="success" @click="exp" class="ml-5">导出 <i class="el-icon-upload2"></i></el-button>
    </div>

    <el-table :data="tableData" border stripe header-cell-class-name="headerBg"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="physician.nickname" label="医生"></el-table-column>
      <el-table-column prop="question.problem" label="问题"></el-table-column>
      <el-table-column prop="answer" label="回复"></el-table-column>
      <el-table-column prop="createtime" label="回复时间"></el-table-column>
      <el-table-column label="操作" width="180px">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
          <el-button type="danger" @click="del(scope.row.id)">删除 <i class="el-icon-remove-outline"></i></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <el-dialog title="回复信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="80px" size="small">
        <el-form-item label="医生">
          <el-select clearable v-model="form.phyId" placeholder="请选择">
            <el-option v-for="item in physicians" :key="item.id" :label="item.nickname" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="问题">
          <el-select clearable v-model="form.queId" placeholder="请选择">
            <el-option type="textarea" v-for="item in questions" :key="item.id" :label="item.problem" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="回复">
          <el-input type="textarea" v-model="form.answer" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="success" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Reply",
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      answer: "",
      username: "",
      identity: "",
      physicians: {},
      questions: {},
      dialogFormVisible: false,
      form: {},
      multipleSelection: [],
      headerBg: 'headerBg'
    }
  },
  created() {
    //请求分页查询数据
    this.load()
  },
  methods: {
    load() {
      this.request.get("/reply/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          answer: this.answer
        }
      }).then(res => {
        console.log("值："+res)
        this.tableData = res.data
        this.total = res.total
      })
    },
    save() {
      this.request.post("/reply/edit", this.form).then(res => {
        if (res) {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    handleAdd() {
      this.request.get("/physician/list").then(res => {
        console.log(res)
        this.physicians = res
      })
      this.request.get("/question/list").then(res => {
        console.log(res)
        this.questions = res
      })
      this.dialogFormVisible = true
      this.form = {}
    },
    handleEdit(row) {
      this.request.get("/physician/list").then(res => {
        console.log(res)
        this.physicians = res
      })
      this.request.get("/question/list").then(res => {
        console.log(res)
        this.questions = res
      })
      this.form = Object.assign({}, row)
      this.dialogFormVisible = true
    },
    del(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.delete("/reply/del?id=" + id).then(res => {
          if (res) {
            this.$message.success("删除成功")
            this.load()
          } else {
            this.$message.error("删除失败")
          }
        })
      });
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    deleteBatch() {
      let ids = this.multipleSelection.map(v => v.id) //将对象数组变成纯id数组 [{},{},{}] => [1,2,3]
      this.$confirm('此操作将永久批量删除选中数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.delete("/reply/delBatch", {data: ids}).then(res => {
          if (res) {
            this.$message.success("批量删除成功")
            this.load()
          } else {
            this.$message.error("批量删除失败")
          }
        })
      });
    },
    reset() {
      this.answer = ""
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
    exp() {
      window.open("http://localhost:8082/reply/export")
    },
    handleExcelImportSuccess() {
      this.$message.success("导入成功")
      this.load()
    },
    handleAvatarSuccess(res){
      this.form.picture = res
    }
  }
}
</script>

<style scoped>

</style>
